package com.zk5_education.service_edu.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zk5_education.R;
import com.zk5_education.service_edu.entity.EduCourse;
import com.zk5_education.service_edu.entity.vo.AllCourseQuery;
import com.zk5_education.service_edu.entity.vo.CourseMessage;
import com.zk5_education.service_edu.entity.vo.CourseQuery;
import com.zk5_education.service_edu.service.EduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-08
 */
@RestController
@RequestMapping("/service_edu/edu_course")
@CrossOrigin
public class EduCourseController {

    @Autowired
    EduCourseService eduCourseService;
    @PostMapping("addCourse")
    public R addCourse(@RequestBody CourseQuery courseQuery){
        String courseid=eduCourseService.addcourse(courseQuery);
        return R.success().data("courseid",courseid);
    }

    //根据id查询课程的信息
    @GetMapping("getCourse/{courseid}")
    public R getCourseMessage(@PathVariable String courseid ){

        CourseQuery cq=eduCourseService.getcourseMessage(courseid);
        return R.success().data("CourseQuery",cq);
    }
    //更新课程信息

     @PostMapping("UpdateCourse")
    public R UpdateCourse(@RequestBody CourseQuery courseQuery){

        eduCourseService.saveCourse(courseQuery);
        return R.success();
     }

     //发布信息回显

    @PostMapping("selectMessage/{courseid}")
    public R selectmessage(@PathVariable String courseid){
        CourseMessage courseMessage = eduCourseService.selectMessage(courseid);
        return R.success().data("courseMessage",courseMessage);
    }

    //发布课程
    @PostMapping("publish/{courseid}")
    public R publishCourse(@PathVariable String courseid){
//开启事务会比较好
        eduCourseService.publishcourse(courseid);
        return R.success();
    }

    //分页获取所有课程
    @PostMapping("PageCourseCondition/{current}/{limit}")
    public R PageCourseCondition(@PathVariable Long current,
                                 @PathVariable Long limit,
                                 @RequestBody(required = false)  AllCourseQuery allCourseQuery
                                 ){
        Page<EduCourse>page=new Page<>(current,limit);
        eduCourseService.pageCourse(page,allCourseQuery);
        List<EduCourse> list = page.getRecords();
        long total = page.getTotal();
        return R.success().data("list",list).data("total",total);
    }
    //删除课程

    @DeleteMapping("deleteCourse/{courseid}")
    public R DeleteCourse(@PathVariable String courseid){
        eduCourseService.deleteCourse(courseid);

        return R.success();
    }
}

